/**
 * Created by Administrator on 2017/4/14.
 */
"use strict";
swan_pc
    .controller("OrgOppTeamController",function($scope,$q,$rootScope,$state,ngDialog,setOwner,oppOwner,getOrgContactJoinOrg) {

        if ($rootScope.presentAccountInfo.id!=$rootScope.opp.ownerId||!$scope.$parent.isMyOpp){
            $scope.canEdit=false;
        }else {
            $scope.canEdit=true;
        }



        $scope.getOrgContactList=function () {
            oppOwner.query({page:0,size:5000,sort:'asc',id:$rootScope.opp.id},function (eolist) {
                $scope.EOList=eolist;
                getOrgContactJoinOrg.query({page:0,size:5000},function (result) {
                    $scope.friendList=[];
                    var notEO=true;
                    var eoId=null;
                    _.forEach(result,function (friend) {
                        notEO=true;
                        eoId=null;
                        _.forEach($scope.EOList,function (eo) {
                            if(friend.id==eo.orgContactId){
                                notEO=false;
                                eoId=eo.id;
                            }
                        })
                        if (friend.categorys[0].businessScope==32){
                            $scope.friendList.push({
                                id:friend.id,
                                name:friend.baseInfo.name,
                                gender:friend.baseInfo.gender,
                                phone:friend.baseInfo.mobileNumberList[0].value,
                                userId:friend.friendId,
                                notEO:notEO,
                                eoId:eoId,
                                isResponse:friend.friendId==$rootScope.opp.ownerId,
                            })
                        }
                    });
                    console.log("LIST",$scope.friendList)
                })
            })

        };
        $scope.getOrgContactList();


        $scope.createOwner=function () {
            ngDialog.open({
                templateUrl:'scripts/components/organizationBusiness/myOpp/selectEO.html',
                scope:$scope,
                controller:['$scope',function ($scope) {
                    $scope.addEO=function(selectEO){
                        var newEO={
                            id:null,
                            opp:{
                                id:$rootScope.opp.id,
                            },
                            user:{
                                id:selectEO.userId,
                            },
                        }
                        setOwner.save(newEO,function (result) {
                            angular.forEach($scope.friendList,function (f) {
                                if (f.id==selectEO.id){
                                    f.notEO=false;
                                    f.eoId=result.id;
                                }
                            })
                        },function (result) {
                            console.log(result)
                            swal("添加失败","","error")
                        })
                    }
                    $scope.cancel=function(){
                        ngDialog.closeAll();
                    }
                }]
            })
        }

        $scope.removeEO=function (deleteEO) {
            setOwner.delete({id:deleteEO.eoId},function (result) {
                console.log(result);
                angular.forEach($scope.friendList,function (f,index) {
                    if (f.id==deleteEO.id){
                        f.notEO=true;
                    }
                })
            },function (result) {
                console.log(result)
                swal("删除失败","","error")
            })
        }

        //分页控制实现
        $scope.pageSize=10;
        $scope.initPageSort=function(item){
            $scope.data = item;
            $scope.pages = Math.ceil($scope.data.length / $scope.pageSize); //分页数
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            //设置表格数据源(分页)
            $scope.setData = function () {
                $scope.items = $scope.data.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));//通过当前页数筛选出表格当前显示数据
            };
            $scope.items = $scope.data.slice(0, $scope.pageSize);
            //分页要repeat的数组
            for (var i = 0; i < $scope.newPages; i++) {
                $scope.pageList.push(i + 1);
            }
            //打印当前选中页索引
            $scope.selectPage = function (page) {
                //不能小于1大于最大
                if (page < 1 || page > $scope.pages) return;
                //最多显示分页数5
                if (page > 2) {
                    //因为只显示5个页数，大于2页开始分页转换
                    var newpageList = [];
                    for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
                        newpageList.push(i + 1);
                    }
                    $scope.pageList = newpageList;
                }
                $scope.selPage = page;
                $scope.setData();
                $scope.isActivePage(page);
                //console.log("选择的页：" + page);
            }
        };
        //设置当前选中页样式
        $scope.isActivePage = function (page) {
            return $scope.selPage == page;
        };
        //上一页
        $scope.Previous = function () {
            $scope.selectPage($scope.selPage - 1);
        };
        //下一页
        $scope.Next = function () {
            $scope.selectPage($scope.selPage + 1);
        };

    })